package zucc.edu.cn.chengchengparttime.Home;

import android.app.Fragment;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import zucc.edu.cn.chengchengparttime.Home.HomeAdapter;
import zucc.edu.cn.chengchengparttime.MainActivity;
import zucc.edu.cn.chengchengparttime.R;
import zucc.edu.cn.chengchengparttime.Text;
import zucc.edu.cn.chengchengparttime.Work;
import zucc.edu.cn.chengchengparttime.dbHomeHelper;

public class HomeFragment extends Fragment {
    private static final String DEBUG_TAG="Chengcheng Log2";
    private SQLiteDatabase mDatabase;// 数据库实例

    private RecyclerView rv; //RecyclerView布局
    private HomeAdapter homeAdapter; //适配器
    private Context context;
    private List<Work> data=new ArrayList<>(); //数据列表
    private EditText editText;
    private TextView search;

    public HomeFragment() {}

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        context = this.getActivity();
        rv = view.findViewById(R.id.rv);

        mDatabase=SQLiteDatabase.openDatabase("/data/user/0/zucc.edu.cn.chengchengparttime/databases/Chengcheng.db",null,SQLiteDatabase.OPEN_READWRITE);//获取数据库
        Log.d("Chengcheng.db2","获取数据库"+mDatabase);

        //查询兼职信息表的记录
        Cursor c=mDatabase.query("tbl_work", null, null, null, null, null,null);
        c.moveToFirst();//游标移到第一条记录
        // 打印记录
        String[] x2 = new String[30];
        while(!c.isAfterLast()){
            String rowResults = "||";
            for (int i = 0; i < c.getColumnCount(); i++) {
                rowResults = rowResults.concat(c.getString(i) + "||");
                x2[i]=c.getString(i);
            }
            Work work=new Work(Long.parseLong(x2[0]),x2[1],x2[2],x2[3],x2[4],x2[5],Double.parseDouble(x2[6]),x2[7],x2[8],x2[9],Integer.parseInt(x2[10]));
            data.add(work);
            Log.d("work",work.toString());//测试

            c.moveToNext();
        }
        Log.d(DEBUG_TAG,"查询成功");
        c.close();//关闭游标工厂

        //适配器的定义与设置
        homeAdapter=new HomeAdapter(data,context,view,mDatabase);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this.getActivity());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        rv.setLayoutManager(linearLayoutManager);
        rv.setAdapter(homeAdapter);

        editText=view.findViewById(R.id.search);
        editText.setOnClickListener(view1 -> {
            Toast.makeText(this.context, "点击了搜索兼职输入框" , Toast.LENGTH_SHORT).show();
        });

        search=view.findViewById(R.id.textsearch);
        search.setOnClickListener(view1 -> {
            Toast.makeText(this.context, "点击了搜索兼职" , Toast.LENGTH_SHORT).show();
            //关闭手机的键盘
            InputMethodManager imm=(InputMethodManager) view1.getContext().
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm.isActive()) {
                imm.hideSoftInputFromWindow(view1.getApplicationWindowToken(),0 );
            }
            //输入框内容进行模糊查询
//            data.clear();
//            Cursor c2=mDatabase.query("tbl_work", null, "WorkSerials=?", new String[]{search.getText().toString()}, null, null,null);
//            c2.moveToFirst();//游标移到第一条记录
//            // 打印列名
//            String rowHeaders2 = "|| ";
//            for (int i = 0; i < c2.getColumnCount(); i++) {
//                rowHeaders2 = rowHeaders2.concat(c2.getColumnName(i) + " || ");
//            }
//            Log.i(DEBUG_TAG, "COLUMNS " + rowHeaders2);
//            // 打印记录
//            String[] x3 = new String[30];
//            while(!c2.isAfterLast()){
//                String rowResults2 = "||";
//                for (int i = 0; i < c2.getColumnCount(); i++) {
//                    rowResults2 = rowResults2.concat(c2.getString(i) + "||");
//                    x3[i]=c2.getString(i);
//                    Log.d("x3",x3[i]);//测试
//                }
//                Work work=new Work(Long.parseLong(x3[0]),x3[1],x3[2],x3[3],x3[4],x3[5],Double.parseDouble(x3[6]),x3[7],x3[8],x3[9],Integer.parseInt(x3[10]));
//                data.add(work);
//                Log.d("work",work.toString());//测试
//
//                Log.i(DEBUG_TAG, "Row " + c2.getPosition() + ": " + rowResults2);
//                c.moveToNext();
//            }
//            Log.d(DEBUG_TAG,"查询成功");
//            c.close();//关闭游标工厂
//            //更新页面
//            //适配器的定义与设置
//            homeAdapter=new HomeAdapter(data,context,view);
//            LinearLayoutManager linearLayoutManager2=new LinearLayoutManager(this.getActivity());
//            linearLayoutManager2.setOrientation(RecyclerView.VERTICAL);
//            rv.setLayoutManager(linearLayoutManager2);
//            rv.setAdapter(homeAdapter);

            //输入框内容清空
            editText.setText("");
        });
        return view;
    }
}
